#include "../Base.h"
HANDLE gHandle;

VOID initHook() {
	GetUserGetForegrounWindow();
	GetUserFindWindowEx();
	GetNtUserQueryWindow();
	GetNtUserWindowFromPoint();
	GetNtUserBuildHwndList();

}


PVOID MyNtUserGetForegroundWindow() {

	typedef PVOID(NTAPI* NtUserGetForegroundWindowProc)(VOID);
	NtUserGetForegroundWindowProc NtUserGetForegroundWindowFunc = (NtUserGetForegroundWindowProc)GetUserGetForegrounWindow();
	PVOID hwnd = NtUserGetForegroundWindowFunc();
	if (hwnd == gHandle) {
		return NULL;
	}
	return hwnd;
}

PVOID  __fastcall MyNtUserFindWindowEx(PVOID desktop1, PVOID a2, PUNICODE_STRING a3, PUNICODE_STRING a4, ULONG64 u1) {
	typedef PVOID(NTAPI* NtUserFindWindowExProc)(PVOID desktop1, PVOID a2, PUNICODE_STRING a3, PUNICODE_STRING a4, ULONG64 u1);
	NtUserFindWindowExProc NtUserGetForegroundWindowFunc = (NtUserFindWindowExProc)GetUserFindWindowEx();
	PVOID hwnd = NtUserGetForegroundWindowFunc(desktop1, a2, a3, a4, u1);

	if (hwnd == gHandle) {

		return NULL;
	}
	return hwnd;


}


__int64 __fastcall MyNtUserQueryWindow(__int64 windowHandle, int isQueryThread) {

	typedef ULONG_PTR(NTAPI* NtUserQueryWindowProc)(__int64 windowHandle, int isQueryThread);
	NtUserQueryWindowProc NtUserQueryWindowFunc = (NtUserQueryWindowProc)GetUserFindWindowEx();
	if (windowHandle == (ULONG_PTR)gHandle) {
		return 0;
	}
	return NtUserQueryWindowFunc(windowHandle, isQueryThread);

}
PVOID __fastcall MyNtUserWindowFromPoint(PVOID point) {
	typedef ULONG_PTR(NTAPI* NtUserWindowFromPointProc)(PVOID a1);
	NtUserWindowFromPointProc NtUserWindowFromPointFunc = (NtUserWindowFromPointProc)GetUserFindWindowEx();
	HANDLE hwnd = (HANDLE)NtUserWindowFromPointFunc(point);
	if (hwnd == gHandle) {
		return NULL;
	}
	return hwnd;
}
NTSTATUS __fastcall MyNtUserBuildHwndList(
	__int64 parentHwnd,
	__int64 a2,
	int a3,
	int a4,
	int a5,
	unsigned int a6,
	volatile void* Address,
	unsigned int* number) {
	typedef NTSTATUS(NTAPI* NtUserBuildHwndListProc)(
		__int64 parentHwnd,
		__int64 a2,
		int a3,
		int a4,
		int a5,
		unsigned int a6,
		volatile void* Address,
		unsigned int* number);
	NtUserBuildHwndListProc NtUserBuildHwndListFunc = (NtUserBuildHwndListProc)GetUserFindWindowEx();
	NTSTATUS status = NtUserBuildHwndListFunc(
		parentHwnd,
		a2,
		a3,
		a4,
		a5,
		a6,
		Address,
		number
	);

	if (MmIsAddressValid(number)) {
		int scount = *number;
		PVOID* arrays = (PVOID*)Address;
		for (int i = 0; i < *number; i++) {

			if (arrays[i] == gHandle) {
				if (i == 0) {
					//只有1个
					if (scount == 1) {
						arrays[i] = 0;
						*number = 0;
						break;
					}
					arrays[i] = arrays[i + 1];
					break;
				}

			}
			else {
				arrays[i] = arrays[i - 1];
				break;
			}


		}
	}

	if (NT_SUCCESS(status)) {

		LOG("param1:%llx param2:%llx prams3:%x", parentHwnd, a2, a3);
		LOG(" params4:%x params5:%x params6:%x param7:%llx param8:%x", a4, a5, a6, Address, *number);
	}
	return status;

}